<template>
	<view class="container">
		<u-form :model="form" ref="uForm" labelWidth="0">
			<view class="form-subtitle">
				<view class="form-subtitle-left">
					基本信息
				</view>
			</view>
			<view class="form-container">
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">类型<span class="label-required">*</span></span>
						<u--input v-model="form.typeName" @touchstart="handleSelectType" placeholder="请选择类型" readonly
							inputAlign="right" border="none" suffixIcon="arrow-right" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">标题<span class="label-required">*</span></span>
						<u--input v-model="form.title" placeholder="请输入标题" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">内容<span class="label-required">*</span></span>
						<u--input v-model="form.content" placeholder="请输入内容" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex" style="margin-bottom:20rpx;">
						<span class="form-label">图片</span>
						<span>{{imgList.length}}/8</span>
					</view>
					<u-upload uploadIcon="photo" uploadIconColor="#FE4F04" :fileList="imgList" @afterRead="afterRead"
						@delete="deletePic" :maxCount="8" :previewImage="true"></u-upload>
				</u-form-item>
			</view>

			<view class="form-subtitle">
				<view class="form-subtitle-left">
					接收人（不指定时自动按类型匹配）
				</view>
			</view>
			<view class="form-container">
				<u-form-item v-if="buildings.length == 0">
					<view class="form-flex">
						<span class="form-label">客户<span class="label-required">*</span></span>
						<!-- <u--input v-model="customer" placeholder="请选择客户" @touchstart="handleSelectCustomer"
							inputAlign="right" border="none" readonly suffixIcon="arrow-right" /> -->
						<view class="customer-input">
							<view class="" v-if="customer.length == 0" @touchstart="handleSelectCustomer" style="color: #303133;">
								请选择客户
							</view>
							<view class="" v-else style="display: flex; flex-wrap: wrap;">
								<u-tag :text="c.name" plain size="mini" closable v-for="(c,i) in customer"
									@close="delCustomer(i)" color="#000" borderColor="#000" closeColor="#000"></u-tag>
							</view>
							<u-icon name="arrow-right" @touchstart="handleSelectCustomer"></u-icon>
						</view>
					</view>
				</u-form-item>
				<u-form-item v-if="customer.length == 0">
					<view class="form-flex">
						<span class="form-label">大楼<span class="label-required">*</span></span>
						<!-- <u--input v-model="buildingName" placeholder="请选择大楼" @touchstart="handleSelectBuilding"
							inputAlign="right" border="none" readonly suffixIcon="arrow-right" /> -->
						<view class="building-input">
							<view class="" v-if="buildings.length == 0" @touchstart="handleSelectBuilding" style="color: #303133;">
								请选择大楼
							</view>
							<view class="" v-else style="display: flex; flex-wrap: wrap;">
								<u-tag :text="b.address" plain size="mini" closable v-for="(b,i) in buildings"
									@close="delBuilding(i)" color="#000" borderColor="#000" closeColor="#000"></u-tag>
							</view>
							<u-icon name="arrow-right" @touchstart="handleSelectBuilding"></u-icon>
						</view>
					</view>
				</u-form-item>
			</view>
			<view class="form-bottom-opt">
				<view class="opt-template" @click="handleSelectTemplate">
					<u-icon name="grid"></u-icon>
					<span>模版库中选择</span>
				</view>
				<u-button @click="submit" v-show="!form.id" :custom-style="submitBtnStyle">提交</u-button>
			</view>
		</u-form>

		<u-popup :show="showType" :round="20">
			<view class="popup">
				<view class="popup-top">
					<view class="popup-opt-cancel" @click="showType = false">
						取消
					</view>
					<view class="popup-title">
						类型
					</view>
					<view class="popup-opt-confirm" @click="selectTypeConfirm">
						确认
					</view>
				</view>
				<view class="popup-list-container">
					<view class="popup-type-list">
						<view class="popup-type-item" v-for="item in types" :key="item" @click="selectedType = item"
							:class="selectedType.key == item.key ? 'active' : ''">
							{{item.value}}
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>


<script>
	import {
		noticeAdd,
		uploadFile
	} from '@/api/part2.js'
	export default {
		data() {
			return {
				form: {
					type: '',
					typeName: '',
					title: '',
					content: '',
					images: [],
					renter_ids: '',
					build_ids: ''
				},
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写标题',
						trigger: ['blur', 'change']
					},
				},
				imgList: [],
				submitBtnStyle: {
					background: "#FE4F04",
					boShadow: "0rpx 4rpx 8rpx 0rpx rgba(254,79,4,0.5)",
					borderRadius: "44rpx",
					fontSize: "30rpx",
					color: "#FFFFFF",
					height: "88rpx",
				},

				buildings: [],
				customer: [],

				showType: false,
				selectedType: {},
				types: [{
						key: '1',
						value: '通知'
					},
					{
						key: '2',
						value: '抄表'
					},
					{
						key: '3',
						value: '催租'
					},
					{
						key: '4',
						value: '广播'
					}
				],

			};
		},
		onLoad(options) {
			// if(options.id) {
			// 	this.form.id = options.id
			// 	this.getnoticeInfo()
			// }
			uni.$on('customerSelected', (customer) => {
				console.log(customer)
				customer.forEach(c => {
					if (this.customer.findIndex(cc => cc.id == c.id) <= -1) {
						this.customer.push(c)
					}
				})
			});


			uni.$on('buildingSelected', (template) => {
				console.log(template)
				let index = this.buildings.findIndex(b => b.id == template.id)
				if (index <= -1) {
					this.buildings.push(template)
				}
			});
			uni.$on('templateSelected', (template) => {
				console.log(template)
				this.form.type = template.type
				this.form.typeName = template.type_text
				this.form.title = template.title;
				this.form.content = template.content;
			});
		},
		onUnload() {
			uni.$off('customerSelected'); // 避免内存泄漏
			uni.$off('buildingSelected'); // 避免内存泄漏
			uni.$off('templateSelected'); // 避免内存泄漏
		},
		methods: {
			// getnoticeInfo(){
			// 	noticeInfo(this.form.id).then(res=>{
			// 		console.log(res.data.data)
			// 		let data = res.data.data
			// 		this.form = {...data}
			// 	})
			// },
			handleSelectType() {
				console.log(1111)
				this.selectedType = {
					key: this.form.type,
					value: this.form.typeName
				}
				this.showType = true
			},
			handleSelectCustomer() {
				uni.navigateTo({
					url: '/pages-landlord/announcement/chooseCustomer'
				})
			},
			handleSelectBuilding() {
				uni.navigateTo({
					url: '/pages-landlord/announcement/chooseBuilding'
				})
			},
			handleSelectTemplate() {
				uni.navigateTo({
					url: '/pages-landlord/announcement/chooseTemplate'
				})
			},
			delBuilding(i) {
				this.buildings.splice(i, 1)
			},
			delCustomer(i) {
				this.customer.splice(i, 1)
			},
			selectTypeConfirm() {
				this.showType = false
				this.form.type = this.selectedType.key
				this.form.typeName = this.selectedType.value
			},
			afterRead(e) {
				uni.showLoading({
					title: "上传中...",
				});
				this.imgList.push(e.file);
				uploadFile(e.file.url).then((res) => {
					uni.hideLoading();
					this.form.images.push(JSON.parse(res.data).code.fullurl);
				});
			},
			deletePic(e) {
				this.imgList.splice(e.index, 1);
				this.form.images.splice(e.index, 1);
			},
			submit() {
				console.log('提交')
				this.$refs.uForm.validate().then(res => {
					let form = {
						...this.form,
						images : this.form.images.join(','),
						renter_ids : this.customer.map(b=>b.id).join(','),
						build_ids : this.buildings.map(b=>b.id).join(','),
					}
					noticeAdd(form).then(res => {
						uni.$u.toast(res.msg || '添加成功')
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					})
				}).catch(errors => {
					console.log(errors)
					uni.$u.toast('校验失败')
				})
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	};
</script>

<style lang="less" scoped>
	.container {
		padding: 0 32rpx;
		padding-bottom: 150rpx;
		margin-bottom: constant(safe-area-inset-bottom);
		margin-bottom: env(safe-area-inset-bottom);
		background: #F5F6FA;
		min-height: 100vh;
	}

	.form-subtitle {
		padding: 24rpx 40rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 700;
		font-size: 26rpx;
		color: #2A0C00;
		line-height: 36rpx;

		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.form-container {
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.01);
		border-radius: 16rpx;
		padding: 0 40rpx;
		margin-bottom: 20rpx;

		.form-flex {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.form-label {
			margin-right: 10rpx;

			.label-required {
				color: #FF0000;
				padding: 0 4rpx;
			}
		}
		
		.customer-input, .building-input {
			display: flex;
			align-items: center;
		}
	}

	.form-bottom-opt {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 1000;
		/* 关键样式 - 使用 constant 和 env 兼容不同设备 */
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS < 11.2 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iOS >= 11.2 */
		/* 其他样式按需添加 */
		background-color: #F5F6FA;
		padding: 20rpx 40rpx;
		display: flex;
		gap: 48rpx;

		.opt-template {
			min-width: 132rpx;
			height: 32rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 22rpx;
			color: #333333;
			line-height: 32rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 44px;
		}
	}


	.popup {
		padding: 40rpx;

		font-size: 32rpx;

		.popup-top {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-family: PingFangSC, PingFang SC;
			margin-bottom: 48rpx;

			.popup-opt-cancel {
				color: #999999;
			}

			.popup-title {
				color: #222222;
			}

			.popup-opt-confirm {
				color: #FE4F04;
			}
		}

		.popup-list-container {
			min-height: 300rpx;
			padding-bottom: env(safe-area-inset-bottom);
		}

		.popup-type-list {
			display: flex;
			flex-wrap: wrap;
			gap: 30rpx 24rpx;

			.popup-type-item {
				width: 200rpx;
				height: 80rpx;
				background: #F5F6FA;
				border-radius: 40rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 28rpx;
				color: #222222;
				border: 1rpx solid #F5F6FA;
			}

			.popup-type-item.active {
				border: 1rpx solid #FE4F04;
				color: #FF7D41;
				background: #FFF6ED;
			}
		}
	}

	/deep/ .u-form-item {
		justify-content: flex-end !important;
		/* 右对齐 */
	}

	/deep/ .u-radio-group--row {
		gap: 30rpx;
		justify-content: flex-end;
		align-items: center;
	}
</style>